#include "key_scan.h"
#include "delay.h"

void KEY_ScanInit(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;

  /* config the GPIO Clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
 
  /* gpio config(PD8,PD9,PD10,PD11) */ 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4|
                                GPIO_Pin_5|GPIO_Pin_6; 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 
  GPIO_Init(GPIOA, &GPIO_InitStructure);

}

u8 KEY_Scan(u8 mode) 
{ 
  static u8 key_up = 1;
  if(mode) 
  { 
    key_up = 1;
  } 
  if(key_up && (KEY1 == 0||KEY2 == 0||KEY3 == 0||KEY4 == 0)) 
  { 
    delay_ms(10); 
    key_up = 0; 
    if(KEY1 == 0) return KV_SUB; 
    else if(KEY2 == 0) return KV_ADD; 
    else if(KEY3 == 0) return KV_MOD; 
    else if(KEY4 == 0) return KV_ERA; 
  }else if((KEY1 == 1) && (KEY2 == 1) && (KEY3 == 1) && (KEY4 == 1)) 
  { 
    key_up = 1; 
  } 
  return 0;
 }


